﻿using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Linq.Dynamic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.ComponentModel;
using System.Linq.Expressions;

namespace Catalyst.Web.DynamicData
{
    /// <summary>
    /// A parameter responsible for creating a LambdaExpression that performs an 'in' comparisson.
    /// </summary>
    public class InExpressionParameter : DynamicExpressionParameterBase
    {
        public InExpressionParameter() { }

        public TypeCode ValueType
        {
            get
            {
                object obj2 = this.ViewState["ValueType"];
                if (obj2 == null)
                {
                    return TypeCode.Empty;
                }
                return (TypeCode)obj2;
            }
            set
            {
                if (this.Type != value)
                {
                    this.ViewState["ValueType"] = value;
                    this.OnParameterChanged();
                }
            }
        }

        public IList<object> Values
        {
            get
            {
                object obj2 = this.ViewState["Values"];
                if (obj2 == null)
                {
                    return null;
                }
                return (IList<object>)obj2;
            }
            set
            {
                if (this.Values != value)
                {
                    this.ViewState["Values"] = value;
                    this.OnParameterChanged();
                }
            }
        }

        public string ValueList
        {
            get
            {
                if (Values == null || Values.Count == 0)
                    return String.Empty;
                
                var list = from value in Values
                           select value.ToString();

                return string.Join(",", list.ToArray());
            }
            set
            {
                string[] values = value.Split(',');

                var list = from val in values
                           where !string.IsNullOrEmpty(val)
                           select GetValue(val, "", ValueType, true, false);

                Values = new List<object>(list);
            }
        }

        public override LambdaExpression GetLambdaExpression(Type itType)
        {
            var list = Values;
            if (list == null || list.Count == 0)
                return null;

            var sb = new StringBuilder();
            for (int i = 0; i < list.Count; i++)
            {
                sb.AppendFormat("{0}{1} == @{2}", (i > 0 ? " OR " : ""), this.Name, i);
            }

            return System.Linq.Dynamic.DynamicExpression.ParseLambda(itType, typeof(bool), sb.ToString(), list.ToArray());
        }
    }
}
